package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.entity.SysDictData;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.DictUtils;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallIntegralGoods;
import com.tencreat.domain.mall.dto.MallIntegralGoodsDTO;
import com.tencreat.domain.mall.vo.IntegralChangeVO;
import com.tencreat.domain.mall.vo.IntegralStatisticsVO;
import com.tencreat.mapper.system.SysDictDataMapper;
import com.tencreat.service.mall.IMallIntegralGoodsService;
import com.tencreat.service.system.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 积分商品Controller
 *
 * @author tencreat
 * @date 2021-09-01
 */
@Api(tags = "管理端积分商品api")
//@RestController
@RequestMapping("/mall/integral/goods")
public class AdminMallIntegralGoodsController extends BaseController {
    //@Autowired
    private IMallIntegralGoodsService mallIntegralGoodsService;
    //@Autowired
    private SysDictDataMapper sysDictDataMapper;


    @ApiOperation("PC积分商品列表（新）")
    @GetMapping("/pcList")
    public HttpResult<IPage<Map<String, Object>>> pcList(@ApiParam("商品名称") @RequestParam(required = false) String goodsName,
                                                         @ApiParam("开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime,
                                                         @ApiParam("结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
                                                         @ApiParam("积分数") @RequestParam(required = false) Long count,
                                                         @ApiParam("专区名称") @RequestParam(required = false) Integer zoneId,
                                                         @ApiParam("排序字段") @RequestParam(required = false) String sortStr,
                                                         @ApiParam("排序规则") @RequestParam(required = false) String sortDesc,
                                                         @ApiParam("商品ID") @RequestParam(required = false) Integer goodsId,
                                                         @ApiParam("供应商名称") @RequestParam(required = false) String supplierName,
                                                         @RequestParam Integer pageNum,
                                                         @RequestParam Integer pageSize) {
        Page<Map<String, Object>> page = new Page<>(pageNum, pageSize);
        return HttpResult.ok(mallIntegralGoodsService.pcList(page, goodsName, count, beginTime, endTime,zoneId,sortStr,sortDesc,goodsId,supplierName));
    }

    @ApiOperation("PC新增积分商品（新)")
    @Log(title = "营销管理>积分管理", businessType = BusinessType.INSERT)
    @PostMapping("/new/add")
    public HttpResult<Boolean> add1(@RequestBody List<MallIntegralGoods> mallIntegralGoods) {
        return to(mallIntegralGoodsService.insert(mallIntegralGoods));
    }


    /**
     * 查询积分商品列表
     */
    @ApiOperation(value = "查询积分商品列表")
    @PreAuthorize("@ss.hasPermi('mall:goods:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallIntegralGoods>> list(@RequestBody MallIntegralGoods mallIntegralGoods) {
        QueryWrapper<MallIntegralGoods> wrapper = Wrappers.query();
        if (StringUtils.isNotBlank(mallIntegralGoods.getGoodsName())) {
            wrapper.lambda().like(MallIntegralGoods::getGoodsName, mallIntegralGoods.getGoodsName());
        }
        if (StringUtils.isNotNull(mallIntegralGoods.getCategoryId())) {
            wrapper.lambda().eq(MallIntegralGoods::getCategoryId, mallIntegralGoods.getCategoryId());
        }

        Page<MallIntegralGoods> page = new Page<>(mallIntegralGoods.getPageNum(), mallIntegralGoods.getPageSize());
        return HttpResult.ok(mallIntegralGoodsService.page(page, wrapper));
    }

    /**
     * 导出积分商品列表
     */
    @ApiOperation(value = "导出积分商品列表")
    @PreAuthorize("@ss.hasPermi('mall:goods:export')")
    @Log(title = "营销管理>积分管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallIntegralGoods mallIntegralGoods) {
        List<MallIntegralGoods> list = mallIntegralGoodsService.selectMallIntegralGoodsList(mallIntegralGoods);
        ExcelUtil<MallIntegralGoods> util = new ExcelUtil<MallIntegralGoods>(MallIntegralGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取积分商品详细信息
     */
    @ApiOperation(value = "获取积分商品详细信息")
    @PreAuthorize("@ss.hasPermi('mall:goods:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallIntegralGoodsDTO> getInfo(@RequestBody ReqIdParam id) {
        return HttpResult.ok(mallIntegralGoodsService.selectMallIntegralGoodsById(id));
    }

    /**
     * 新增积分商品
     */
    @ApiOperation(value = "新增积分商品")
    @PreAuthorize("@ss.hasPermi('mall:goods:add')")
    @Log(title = "营销管理>积分管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody MallIntegralGoodsDTO mallIntegralGoodsPo) {
        return to(mallIntegralGoodsService.insertMallIntegralGoods(mallIntegralGoodsPo));
    }

    /**
     * 修改积分商品
     */
    @ApiOperation(value = "修改积分商品")
    @PreAuthorize("@ss.hasPermi('mall:goods:edit')")
    @Log(title = "营销管理>积分管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody MallIntegralGoods mallIntegralGoods) {
        return to(mallIntegralGoodsService.updateMallIntegralGoods(mallIntegralGoods));
    }

    /**
     * 删除积分商品
     */
    @ApiOperation(value = "删除积分商品")
    @PreAuthorize("@ss.hasPermi('mall:goods:remove')")
    @Log(title = "营销管理>积分管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody ReqIdsParam ids) {
        return to(mallIntegralGoodsService.deleteMallIntegralGoodsByIds(ids));
    }


    @ApiOperation(value = "更新商品")
    @PreAuthorize("@ss.hasAnyPermi('mall:goods:update')")
    @Log(title = "营销管理>积分管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateGoodsPo")
    public HttpResult<Boolean> updateGoodsPo(@RequestBody MallIntegralGoodsDTO mallIntegralGoodsPo) {
        return to(mallIntegralGoodsService.updateGoodsPo(mallIntegralGoodsPo));
    }

    /*****
     *  将当前分类的排序减1 顺序上升一位
     * @param mallCategory
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    public HttpResult<Boolean> reSortOrderReduce(@RequestBody MallIntegralGoods mallCategory){
        return HttpResult.ok(mallIntegralGoodsService.reSortOrderReduce(mallCategory));
    }

    /*****
     * 将当前分类的排序加1 顺序下降一位
     * @param mallCategory
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    public HttpResult<Boolean> reSortOrderPlus(@RequestBody MallIntegralGoods mallCategory){
        return HttpResult.ok(mallIntegralGoodsService.reSortOrderPlus(mallCategory));
    }


    @ApiOperation(value="积分统计")
    @GetMapping("/statistics")
    public HttpResult<IntegralChangeVO> statistics() {
        return HttpResult.ok(mallIntegralGoodsService.statistics());
    }

    @ApiOperation(value="积分汇率查询")
    @GetMapping("/integralConfig")
    public HttpResult<String> integralConfig() {
        return HttpResult.ok(DictUtils.getDictValue("integral_rate_config","rate_config"));
    }

    @ApiOperation(value="积分汇率修改")
    @Log(title = "营销管理>积分管理", businessType = BusinessType.UPDATE)
    @PostMapping("/configUpdate")
    public HttpResult<String> integralConfig(@RequestBody SysDictData sysDictData) {
        // 更改
        int type = sysDictDataMapper.updateByType(sysDictData.getDictValue(), "integral_rate_config", "rate_config");
        if (type >0) {
            DictUtils.clearDictCache("integral_rate_config");
        }
        return HttpResult.ok();
    }
}
